﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PookSistener.Product
{
    public partial class frmProductAdd : Form
    {
        OleDbConnection conn = null;
        internal string MODE = "";

        List<SystemConfig.Category> category = new List<SystemConfig.Category>();
        List<SystemConfig.Category> subCategory = new List<SystemConfig.Category>();

        public frmProductAdd(OleDbConnection conn)
        {
            this.conn = conn;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
            if (conn != null)
            {
                conn.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MODE.Equals("ADD"))
            {
                string sql = "";
                sql = "SELECT ID FROM Product ORDER BY ID DESC";

                OleDbCommand sCmd = new OleDbCommand();
                sCmd.CommandType = CommandType.Text;
                sCmd.CommandText = sql;
                sCmd.Connection = conn;

                OleDbDataReader reader = sCmd.ExecuteReader();

                int product_id = 1;

                if (reader.HasRows)
                {
                    try
                    {
                        reader.Read();

                        product_id = Convert.ToInt32(reader.GetString(0));

                        product_id++;
                    }
                    catch (Exception ex)
                    {
                        product_id = 1;
                    }
                }

                sql = "INSERT INTO Product(ID, Name, Barcode, SubCategory, Category, Unit) VALUES (@ID, @Name, @Barcode, @SubCategory, @Category, @Unit)";

                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Connection = conn;
                cmd.Parameters.Clear();

                cmd.Parameters.Add("@ID", OleDbType.Integer).Value = product_id;
                cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtName.Text;
                cmd.Parameters.Add("@Barcode", OleDbType.VarChar).Value = txtBarcode.Text;
                cmd.Parameters.Add("@SubCategory", OleDbType.Integer).Value = Convert.ToInt32(cboSubCategory.SelectedValue);
                cmd.Parameters.Add("@Category", OleDbType.Integer).Value = Convert.ToInt32(cboCategory.SelectedValue);
                cmd.Parameters.Add("@Unit", OleDbType.Double).Value = txtUnit.Text;

                cmd.ExecuteNonQuery();

                MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                ClearData();

            }
            else if (MODE.Equals("EDIT"))
            {
                string sql = "UPDATE Product SET Name=@Name, Barcode=@Barcode, SubCategory=@SubCategory, Category=@Category, Unit=@Unit WHERE ID=@ID";

                try
                {
                    int product_id = Convert.ToInt32(txtID.Text);

                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql;
                    cmd.Connection = conn;
                    cmd.Parameters.Clear();

                    cmd.Parameters.Add("@ID", OleDbType.Integer).Value = product_id;
                    cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@Barcode", OleDbType.VarChar).Value = txtBarcode.Text;
                    cmd.Parameters.Add("@SubCategory", OleDbType.Integer).Value = Convert.ToInt32(cboSubCategory.SelectedValue);
                    cmd.Parameters.Add("@Category", OleDbType.Integer).Value = Convert.ToInt32(cboCategory.SelectedValue);
                    cmd.Parameters.Add("@Unit", OleDbType.Double).Value = txtUnit.Text;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");
                    this.Close();
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("เกิดข้อผิดพลาด [" + ex.Message + "]");
                    this.Close();
                }
            }
        }

        private void ClearData()
        {
            txtID.Text = "";
            txtName.Text = "";
            txtUnit.Text = "0";
            txtBarcode.Text = "";
            cboSubCategory.SelectedValue = "";
            cboCategory.SelectedValue = "";
        }

        private void ListCategory()
        {
            category.Clear();
            subCategory.Clear();

            string sql = "SELECT * FROM Category";

            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Connection = conn;
            cmd.Parameters.Clear();


            OleDbDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                SystemConfig.Category item = new SystemConfig.Category();
                item.ID = reader.GetInt32(0);
                item.Name = reader.GetString(1);

                category.Add(item);
            }

            reader.Close();

            sql = "SELECT * FROM SubCategory";
            cmd = new OleDbCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = sql;
            cmd.Connection = conn;
            cmd.Parameters.Clear();

            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                SystemConfig.Category item = new SystemConfig.Category();
                item.ID = reader.GetInt32(0);
                item.Name = reader.GetString(1);

                subCategory.Add(item);
            }

            reader.Close();

            cboCategory.DataSource = category;
            cboCategory.DisplayMember = "Name";
            cboCategory.ValueMember = "ID";

            cboSubCategory.DataSource = subCategory;
            cboSubCategory.DisplayMember = "Name";
            cboSubCategory.ValueMember = "ID";
        }

        private void frmProductAdd_Load(object sender, EventArgs e)
        {
            ListCategory();
        }
    }
}
